package com.luckeys.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.luckeys.dao.ActivityDao;
import com.luckeys.dao.EventsDao;
import com.luckeys.ios.vo.IosActivityVO;
import com.luckeys.ios.vo.IosCodeVO;
import com.luckeys.ios.vo.IosCommentVO;
import com.luckeys.ios.vo.IosShareVO;
import com.luckeys.ios.vo.IosUserVO;
import com.luckeys.service.ActivityService;
import com.luckeys.vo.ActivityVO;
import com.luckeys.vo.EventsVO;
import com.luckeys.vo.PageVO;

/**
 * 型号实现类
 * 
 * @author tbh
 *
 */
@Service
public class ActivityServiceImpl implements ActivityService {

	@Autowired
	private ActivityDao activityDao;

	@Autowired
	private EventsDao eventsDao;

	public List<ActivityVO> queryActivityList(ActivityVO activityVO) {
		return activityDao.queryActivityList(activityVO);
	}

	public PageVO<ActivityVO> queryActivityPage(ActivityVO activity) {
		PageVO<ActivityVO> page = new PageVO<ActivityVO>();
		page.setData(activityDao.queryActivityPage(activity));
		page.setTotalRecords(activityDao.queryActivityCount(activity));
		return page;
	}

	public PageVO<IosActivityVO> queryIosPage(ActivityVO activity) {
		PageVO<IosActivityVO> page = new PageVO<IosActivityVO>();
		page.setStartIndex(activity.getStartIndex());
		page.setPageSize(activity.getPageSize());
		page.setData(activityDao.queryIosPage(activity));
		page.setTotalRecords(activityDao.queryActivityCount(activity));
		return page;
	}

	public PageVO<IosShareVO> querySharePage(ActivityVO activity) {
		PageVO<IosShareVO> page = new PageVO<IosShareVO>();
		List<IosShareVO> listShare = activityDao.querySharePage(activity);
		if (!CollectionUtils.isEmpty(listShare)) {
			for (IosShareVO share : listShare) {
				share.setListComment(activityDao.queryShareComment(share));
			}
		}
		page.setStartIndex(activity.getStartIndex());
		page.setPageSize(activity.getPageSize());
		page.setData(listShare);
		page.setTotalRecords(activityDao.queryShareCount(activity));
		return page;
	}

	public PageVO<IosActivityVO> queryCollectPage(ActivityVO activity) {
		PageVO<IosActivityVO> page = new PageVO<IosActivityVO>();
		page.setStartIndex(activity.getStartIndex());
		page.setPageSize(activity.getPageSize());
		page.setData(activityDao.queryCollectPage(activity));
		page.setTotalRecords(activityDao.queryCollectCount(activity));
		return page;
	}

	public int addActivity(ActivityVO activity) {
		if (!CollectionUtils.isEmpty(activity.getListImage())) {
			for (String image : activity.getListImage()) {
				ActivityVO imageUrl = new ActivityVO();
				imageUrl.setImageUrl(image);
				imageUrl.setImageUuid(UUID.randomUUID().toString());
				imageUrl.setActivityUuid(activity.getActivityUuid());
				activityDao.addImage(imageUrl);
			}
		}
		if (!CollectionUtils.isEmpty(activity.getListCode())) {
			for (String code : activity.getListCode()) {
				ActivityVO codes = new ActivityVO();
				codes.setActivityUuid(activity.getActivityUuid());
				codes.setCode(code);
				codes.setCodeUuid(UUID.randomUUID().toString());
				activityDao.addCode(codes);
			}
		}
		return activityDao.addActivity(activity);
	}

	public int deleteActivity(String activityUuid) {
		return activityDao.deleteActivity(activityUuid);
	}

	public ActivityVO queryActivityById(String activityUuid) {
		return activityDao.queryActivityById(activityUuid);
	}

	public int updateActivity(ActivityVO Activity) {
		return activityDao.updateActivity(Activity);
	}

	@Override
	public List<IosActivityVO> queryWinByUserId(ActivityVO activity) {
		if (StringUtils.isBlank(activity.getSeeUserUuid())) {
			activity.setSeeUserUuid(activity.getUserUuid());
		}
		List<IosActivityVO> listActivity = activityDao
				.queryWinByUserId(activity);
		if (null != listActivity && listActivity.size() > 0) {
			for (IosActivityVO activityVO : listActivity) {
				// 获取中奖人信息
				activityVO.setWinUser(activityDao.queryWinUser(activityVO));
				// if ("3".equals(activityVO.getActivityStatus())
				// && "1".equals(activityVO.getChipsStatus())) {
				// 如果请求时间不在5分钟之内，则设置倒计时为0
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(activityVO.getEndDate());
				calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 5);
				if (new Date().before(calendar.getTime())) {
					long diff = calendar.getTime().getTime()
							- new Date().getTime();
					long seconds = diff / 1000;
					activityVO.setCountDown(String.valueOf(seconds));
				} else {
					activityVO.setCountDown("0");
				}
				// }
			}
		}
		return listActivity;
	}

	@Override
	public List<IosActivityVO> queryBetByUserId(ActivityVO activity) {
		List<IosActivityVO> listActivity = activityDao
				.queryBetByUserId(activity);
		if (null != listActivity && listActivity.size() > 0) {
			for (IosActivityVO activityVO : listActivity) {
				// 获取中奖人信息
				if ("3".equals(activityVO.getActivityStatus())
						&& "1".equals(activityVO.getChipsStatus())) {
					// 如果请求时间不在5分钟之内，则设置倒计时为0
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(activityVO.getEndDate());
					calendar.set(Calendar.MINUTE,
							calendar.get(Calendar.MINUTE) + 5);
					if (new Date().before(calendar.getTime())) {
						long diff = calendar.getTime().getTime()
								- new Date().getTime();
						long seconds = diff / 1000;
						activityVO.setCountDown(String.valueOf(seconds));
					} else {
						activityVO.setCountDown("0");
					}
				}
				activity.setActivityUuid(activityVO.getActivityUuid());
				activityVO.setListCode(activityDao.queryBetCode(activity));
			}
		}
		return listActivity;
	}

	@Override
	public IosActivityVO queryIosActivityById(ActivityVO activityVOs) {
		IosActivityVO activity = activityDao.queryIosActivityById(activityVOs);
		// 如果是活动结束并且是众筹成功的。则设置倒计时及中奖者信息
//		if ("3".equals(activity.getActivityStatus())
//				&& "1".equals(activity.getChipsStatus())) {
		if ( "1".equals(activity.getChipsStatus())) {
			//如果请求时间不在5分钟之内，则设置倒计时为0
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(activity.getEndDate());
			calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+5);
			if(new Date().before(calendar.getTime()))
			{
				 long diff = calendar.getTime().getTime() - new Date().getTime();
				 long seconds = diff/1000;
				 activity.setCountDown(String.valueOf(seconds));
			}else{
				activity.setCountDown("0");
			}
			//获取中奖人信息
			activity.setWinUser(activityDao.queryWinUser(activity));
		}
		activity.setListBetUser(activityDao.queryBetUser(activity));
		IosActivityVO activityVO = new IosActivityVO();
		activityVO.setActivityStatus(activity.getActivityStatus());
		activityVO.setActivityTypeUuid(activity.getActivityTypeUuid());
		activityVO.setActivityUuid(activity.getActivityUuid());
		activity.setListActivity(activityDao.queryIosActivity(activityVO));
		activity.setListCommentUser(activityDao.queryCommentUser(activityVOs));
		return activity;
	}

	@Override
	public int operCollection(ActivityVO activity) {
		// 0为为未收藏
		if (0 == activity.getiLike()) {
			activity.setCollectUuid(UUID.randomUUID().toString());
			return activityDao.addCollection(activity);
		} else {
			return activityDao.deleteCollection(activity);
		}
	}

	@Override
	public List<IosCodeVO> queryNotUserCode(ActivityVO activity) {
		return activityDao.queryNotUserCode(activity);
	}

	@Override
	public List<IosCodeVO> queryCodeByOutTradeNo(ActivityVO activity) {
		return activityDao.queryCodeByOutTradeNo(activity);
	}

	@Override
	public List<IosCodeVO> queryListCode(ActivityVO activity) {
		return activityDao.queryListCode(activity);
	}

	@Override
	public List<String> betCode(ActivityVO activity) {
		List<String> listCode = new ArrayList<String>();
		if (null != activity) {
			for (String codeUuid : activity.getListCodeUuid()) {
				ActivityVO activityVO = new ActivityVO();
				activityVO.setCodeUuid(codeUuid);
				activityVO.setUserUuid(activity.getUserUuid());
				// 2：提交锁定
				activityVO.setIsWin(activity.getIsWin());
				// 0：未锁定
				activityVO.setOldIsWin(activity.getOldIsWin());
				activityVO.setOutTradeNo(activity.getOutTradeNo());
				activityVO.setPrepayId(activity.getPrepayId());
				int result = activityDao.betCode(activityVO);
				if (result == 0) {
					ActivityVO activit = activityDao.queryCodeById(activityVO);
					listCode.add(activit.getCode());
				}
			}
		}
		return listCode;
	}

	@Override
	public List<String> queryBetCode(ActivityVO activity) {
		List<String> listCode = new ArrayList<String>();
		// if (null != activity) {
		// for (String codeUuid : activity.getListCodeUuid()) {
		// ActivityVO activityVO = new ActivityVO();
		// activityVO.setCodeUuid(codeUuid);
		// activityVO.setUserUuid(activity.getUserUuid());
		// activityVO.setIsWin("1");
		// int result = activityDao.betCode(activityVO);
		// if(result ==0)
		// {
		// ActivityVO activit = activityDao.queryCodeById(activityVO);
		// listCode.add(activit.getCode());
		// }
		// }
		// }
		return listCode;
	}

	@Override
	public int updateCode(IosCodeVO code) {
		ActivityVO activityVO = new ActivityVO();
		activityVO.setCodeUuid(code.getCodeUuid());
		activityVO.setIsWin("1");
		activityVO.setComment(code.getCode());
		return activityDao.updateCode(activityVO);
	}

	@Override
	public int shareActivity(ActivityVO activity) {
		activity.setIsWin("1");
		ActivityVO codeUUid = activityDao.queryCode(activity);
		activityDao.deleteShareImage(codeUUid);
		if (!CollectionUtils.isEmpty(activity.getListImage())
				&& activity.getListImage().size() > 0) {
			for (String image : activity.getListImage()) {
				ActivityVO activityVO = new ActivityVO();
				activityVO.setImageUrl(image);
				activityVO.setShareUuid(UUID.randomUUID().toString());
				activityVO.setCodeUuid(codeUUid.getCodeUuid());
				activityDao.addShareImage(activityVO);
			}
		}
		return activityDao.shareActivity(activity);
	}

	@Override
	public int addComment(ActivityVO activity) {
		return activityDao.addComment(activity);
	}

	@Override
	public PageVO<IosCommentVO> queryCommentPage(ActivityVO activity) {
		PageVO<IosCommentVO> page = new PageVO<IosCommentVO>();
		page.setData(activityDao.queryCommentPage(activity));
		page.setTotalRecords(activityDao.queryCommentCount(activity));
		return page;
	}

	@Override
	public int addUse(ActivityVO activity) {
		return activityDao.addUse(activity);
	}

	@Override
	public int praiseWin(ActivityVO activity) {
		return activityDao.praiseWin(activity);
	}

	@Override
	public PageVO<IosUserVO> queryPraisePage(ActivityVO activity) {
		PageVO<IosUserVO> page = new PageVO<IosUserVO>();
		page.setData(activityDao.queryPraisePage(activity));
		page.setTotalRecords(activityDao.queryPraiseCount(activity));
		return page;
	}

	@Override
	public int paySuccess(ActivityVO activity) {
		return activityDao.paySuccess(activity);
	}

	@Override
	public EventsVO queryEvents(EventsVO events) {
		return eventsDao.queryEvents(events);
	}

	@Override
	public int updateEvents(EventsVO events) {
		return eventsDao.updateEvents(events);
	}

	@Override
	public int addEvents(EventsVO events) {
		return eventsDao.addEvents(events);
	}

	@Override
	public List<IosActivityVO> queryOrderList(ActivityVO activityVO) {
		List<IosActivityVO> listActivity = activityDao.queryOrderList(activityVO);
		if(!CollectionUtils.isEmpty(listActivity)&&listActivity.size()>0)
		{
			for(IosActivityVO activity:listActivity)
			{
				activity.setUserUuid(activityVO.getUserUuid());
				List<IosCodeVO> listCodes = activityDao.queryListCodes(activity);
				if(!CollectionUtils.isEmpty(listCodes)&&listCodes.size()>0)
				{
					activity.setListCodes(listCodes);
				}
			}
		}
		return listActivity;
	}

}
